<!-- Any copyright is dedicated to the Public Domain.
     http://creativecommons.org/publicdomain/zero/1.0/ -->
<!doctype html>

<html>
  <head>
    <meta charset="utf-8"/>
    <title>WebGL editor test page</title>
  </head>

  <body>
    <canvas id="canvas" width="128" height="128"></canvas>
    <script id="shader-fs" type="x-shader/x-fragment">
      precision mediump float;
      uniform vec4 mtrColor;

      void main(void) {
        gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0) * mtrColor;
      }
    </script>
    <script id="shader-vs" type="x-shader/x-vertex">
      attribute vec3 aVertexPosition;

      void main(void) {
        gl_PointSize = 5.0;
        gl_Position = vec4(aVertexPosition, 1.0);
      }
    </script>
    <script type="text/javascript;version=1.8">
      "use strict";

      let canvas, gl, shaderProgram;
      let triangleVertexPositionBuffer, squareVertexPositionBuffer;
     
      window.onload = function() {
        canvas = document.querySelector("canvas");
        gl = canvas.getContext("webgl", { preserveDrawingBuffer: true });
        gl.viewportWidth = canvas.width;
        gl.viewportHeight = canvas.height;
       
        initShaders();
        initBuffers();

        gl.viewport(0, 0, gl.viewportWidth, gl.viewportHeight);
        gl.clearColor(0.0, 0.0, 0.0, 1.0);
        gl.disable(gl.DEPTH_TEST);
        drawScene();
      }

      function getShader(gl, id) {
        var shaderScript = document.getElementById(id);
        if (!shaderScript) {
            return null;
        }

        var str = "";
        var k = shaderScript.firstChild;
        while (k) {
            if (k.nodeType == 3) {
                str += k.textContent;
            }
            k = k.nextSibling;
        }

        var shader;
        if (shaderScript.type == "x-shader/x-fragment") {
            shader = gl.createShader(gl.FRAGMENT_SHADER);
        } else if (shaderScript.type == "x-shader/x-vertex") {
            shader = gl.createShader(gl.VERTEX_SHADER);
        } else {
            return null;
        }

        gl.shaderSource(shader, str);
        gl.compileShader(shader);

        if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
            alert(gl.getShaderInfoLog(shader));
            return null;
        }

        return shader;
      }
      
      function initShaders() {
        var fragmentShader = getShader(gl, "shader-fs");
        var vertexShader = getShader(gl, "shader-vs");

        shaderProgram = gl.createProgram();
        gl.attachShader(shaderProgram, vertexShader);
        gl.attachShader(shaderProgram, fragmentShader);
        gl.linkProgram(shaderProgram);

        if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) {
            alert("Could not initialise shaders");
        }

        gl.useProgram(shaderProgram);

        shaderProgram.vertexPositionAttribute = gl.getAttribLocation(shaderProgram, "aVertexPosition");
        shaderProgram.pMaterialColor = gl.getUniformLocation(shaderProgram, "mtrColor");
        gl.enableVertexAttribArray(shaderProgram.vertexPositionAttribute);
      }

      function initBuffers() {
          // Create triangle vertex/index buffer
          triangleVertexPositionBuffer = gl.createBuffer();
          gl.bindBuffer(gl.ARRAY_BUFFER, triangleVertexPositionBuffer);
          var vertices = [
               0.0,  0.5,  0.0,
              -0.5, -0.5,  0.0,
               0.5, -0.5,  0.0
          ];
          gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
          triangleVertexPositionBuffer.itemSize = 3;
          triangleVertexPositionBuffer.numItems = 3;

          // Create square vertex/index buffer
          squareVertexPositionBuffer = gl.createBuffer();
          gl.bindBuffer(gl.ARRAY_BUFFER, squareVertexPositionBuffer);
          vertices = [
               0.8,  0.8,  0.0,
              -0.8,  0.8,  0.0,
               0.8, -0.8,  0.0,
              -0.8, -0.8,  0.0
          ];
          gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
          squareVertexPositionBuffer.itemSize = 3;
          squareVertexPositionBuffer.numItems = 4;
      }

      function drawScene() {        
        gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT | gl.STENCIL_BUFFER_BIT);

        // DrawArrays
        // --------------
        // draw square - triangle strip
        gl.bindBuffer(gl.ARRAY_BUFFER, squareVertexPositionBuffer);
        gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, squareVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);
        gl.uniform4f(shaderProgram.pMaterialColor, 1, 1, 1, 1);
        gl.drawArrays(gl.TRIANGLE_STRIP, 0, squareVertexPositionBuffer.numItems);

        // draw square - triangle fan
        gl.bindBuffer(gl.ARRAY_BUFFER, squareVertexPositionBuffer);
        gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, squareVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);
        gl.uniform4f(shaderProgram.pMaterialColor, 0, 1, 0, 1);
        gl.drawArrays(gl.TRIANGLE_FAN, 0, squareVertexPositionBuffer.numItems);

        // draw triangle
        gl.bindBuffer(gl.ARRAY_BUFFER, triangleVertexPositionBuffer);
        gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, triangleVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);
        gl.uniform4f(shaderProgram.pMaterialColor, 1, 0, 0, 1);
        gl.drawArrays(gl.TRIANGLES, 0, triangleVertexPositionBuffer.numItems);

        // draw points
        gl.bindBuffer(gl.ARRAY_BUFFER, squareVertexPositionBuffer);
        gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, squareVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);
        gl.uniform4f(shaderProgram.pMaterialColor, 1, 0, 1, 1);
        gl.drawArrays(gl.POINTS, 0, squareVertexPositionBuffer.numItems);

        // draw lines
        gl.bindBuffer(gl.ARRAY_BUFFER, squareVertexPositionBuffer);
        gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, squareVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);
        gl.uniform4f(shaderProgram.pMaterialColor, 0, 0, 1, 1);
        gl.lineWidth(8.0);
        gl.drawArrays(gl.LINES, 0, squareVertexPositionBuffer.numItems);

        // draw line strip
        gl.bindBuffer(gl.ARRAY_BUFFER, squareVertexPositionBuffer);
        gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, squareVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);
        gl.uniform4f(shaderProgram.pMaterialColor, 0.9, 0.6, 0, 1);
        gl.lineWidth(3.0);
        gl.drawArrays(gl.LINE_STRIP, 0, squareVertexPositionBuffer.numItems);

        // draw line loop
        gl.bindBuffer(gl.ARRAY_BUFFER, triangleVertexPositionBuffer);
        gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, triangleVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);
        gl.uniform4f(shaderProgram.pMaterialColor, 0, 1, 1, 1);
        gl.lineWidth(3.0);
        gl.drawArrays(gl.LINE_LOOP, 0, triangleVertexPositionBuffer.numItems);

        window.requestAnimationFrame(drawScene);
      }
    </script>
  </body>

</html>